package com.yx.learn.util;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.yx.learn.domain.Authority;
import com.yx.learn.domain.Department;

/*
 * 采用递归方式去构造树和生成json数组
 * @梁日宇
 */
public class TreeAndToJsonUtil {
	
	private List<Authority> list;
	TreeMap<Integer, List<Authority>> map;

	public TreeAndToJsonUtil(){}
	
	public TreeAndToJsonUtil(List<Authority> list) {
		super();
		this.list = list;
	}

	//将权限信息构造成树形结构，保存在map
	public void createAuthorityTreeMap() {
		map = new TreeMap<Integer, List<Authority>>();
		List<Authority> tmproot = new ArrayList<Authority>();
		map.put((Integer)(-1), tmproot);
		for(Authority auth: list){
			List<Authority> tmp = null;
			if((tmp = map.get((Integer)auth.getPid())) != null){//寻找auth的父节点Piad
				tmp.add(auth);
			}else{
				tmp = new ArrayList<Authority>();
				tmp.add(auth);
			}
			map.put(auth.getPid(), tmp);
		}
	}

	//将菜单权限树转换成json数组
	public JSONArray authorityTreeMapToJSONArray(Integer key){
		List<Authority> li = map.get(key);
		JSONArray ja = new JSONArray();
		if(li != null){
			for(Authority auth: li){
				JSONArray jsa;
				JSONObject jo = new JSONObject();//auth 的孩子打包 为jo，一层层封装
				if((jsa = authorityTreeMapToJSONArray(auth.getId())) != null){//孩子 有自己的孩子，递归打包
					jo.put("name", auth.getAuthorityName());
					jo.put("submenu", jsa);
				}else{
					jo.put("name", auth.getAuthorityName());
					if(auth.getUrl()==null){
						jo.put("url", "");
					}else{
						jo.put("url", auth.getUrl());
					}
				}
				ja.add(jo);//把这些打包 以后给上一层
			}
			return ja;
		}
		return null;
	}
	
	//将authority转换成JSONObject
	public JSONObject authorityToJsonObject(Authority auth,Boolean hasChildren){
		
		JSONObject jo = new JSONObject();//auth 打包 为jo
		jo.put("id", auth.getId());
		jo.put("pid", auth.getPid());
		jo.put("authorityName", auth.getAuthorityName());
		if(auth.getUrl()==null){
			jo.put("url", "");
		}else{
			jo.put("url", auth.getUrl());
		}
		jo.put("flag", auth.getFlag());
		jo.put("memo", auth.getMemo());
		if(hasChildren){//孩子 有自己的孩子
			jo.put("state", "closed");
		}else{
			jo.put("state", "open");
		}
		return jo;
	}
	
	//将部门类封装成json对象
	public JSONObject departmentToJsonObject(Department depart,Boolean hasChildren){
		JSONObject jo = new JSONObject();
		jo.put("id", depart.getId());
		jo.put("pid", depart.getPid());
		jo.put("deptNo", depart.getDeptNo());
		jo.put("deptName", depart.getDeptName());
		jo.put("companyID", depart.getCompany().getId());
		jo.put("companyName", depart.getCompany().getCompanyName());
		jo.put("memo", depart.getMemo());	
		if(hasChildren){
			jo.put("state", "closed");
		}else{
			jo.put("state", "open");
		}
		System.out.println(jo.toString());			
		return jo;			
	}
	
	/**
	 * 对权限做树状结构显示是调用的
	 * @param auth
	 * @param hasChilder
	 * @return
	 */
	public JSONObject jsonObjectForAuthorityTree(Authority auth, Boolean hasChilder){
		JSONObject jo = new JSONObject();
		jo.put("id", auth.getId());
		jo.put("text", auth.getAuthorityName());
		if(hasChilder){
			jo.put("state", "closed");
		}
		return jo;
	}
	


	
	 


}
